package com.cz.kingdee.prod;


import cn.hutool.json.JSONUtil;
import com.cz.kingdee.KingdeeClient;
import com.cz.kingdee.dto.PrdInstock;
import com.cz.kingdee.dto.PrdMo;
import com.cz.kingdee.dto.PrdMorpt;
import com.kingdee.bos.webapi.entity.OperateParam;
import com.kingdee.bos.webapi.entity.OperatorResult;
import com.kingdee.bos.webapi.entity.QueryParam;
import com.kingdee.bos.webapi.sdk.K3CloudApi;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 根据入库单号删除生产汇报单子
 */
public class DelPrdMorptByNumbersByInStockNo {

    public static void main(String[] args) throws Exception {
        KingdeeClient client = new KingdeeClient();
        //K3CloudApi k3CloudApi = client.getTestKingdeeClient();
        K3CloudApi k3CloudApi = client.getProdKingdeeClient();
        List<String> codes = new ArrayList<>();
        codes.add("SCRKCZ25134922");


        for (String FBillNo : codes) {
            QueryParam queryParam = new QueryParam();
            queryParam.setFormId("PRD_INSTOCK");
            queryParam.setFieldKeys("FMaterialId,FMaterialName,FBillNo,FDate,FMoBillNo,FMoId,FMoEntryId,FRealQty,F_ora_XSDDH,FMoEntrySeq,FMaterialId.FNumber,FApproveDate,FPrdOrgId.FName,FWorkShopId1.FName,FStockId.FName");
            queryParam.setFilterString("FBillNo='" + FBillNo + "'");
            List<PrdInstock> prdInstockList = k3CloudApi.executeBillQuery(queryParam, PrdInstock.class);
            int moEntryId = prdInstockList.get(0).getFMoEntryId();
            System.out.println(moEntryId);


            /**
             QueryParam queryParam1 = new QueryParam();
             queryParam1.setFormId("PRD_MO");
             queryParam1.setFieldKeys("FBillNo,FStatus,FTreeEntity_FEntryId,FTreeEntity_FSeq,FDetailMaterialId");
             queryParam1.setFilterString("FTreeEntity_FEntryId  ="+moEntryId+" ");

             List<PrdMo> prdMoList = k3CloudApi.executeBillQuery(queryParam1, PrdMo.class);
             String sourceBillNo = prdMoList.get(0).getFBillNo();
             System.out.println(FBillNo + " "+sourceBillNo);
             **/

            QueryParam queryParam1 = new QueryParam();
            queryParam1.setFormId("PRD_MORPT");
            queryParam1.setFieldKeys("FID,FBillNo,FCreateDate,FDocumentStatus");
            queryParam1.setFilterString("FMoEntryId =" + moEntryId + " ");
            List<PrdMorpt> result = k3CloudApi.executeBillQuery(queryParam1, PrdMorpt.class);
            for (PrdMorpt prdMorpt : result) {
                String fid = String.valueOf(prdMorpt.getFID());

                OperateParam operateParam = new OperateParam();
                operateParam.setIds(fid);
                OperatorResult operatorResult = null;
                try {
                    operatorResult = k3CloudApi.unAudit("PRD_MORPT", operateParam);
                    System.out.println("反审核:" + JSONUtil.toJsonStr(operatorResult.getResult().getResponseStatus()));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    operatorResult = k3CloudApi.delete("PRD_MORPT", operateParam);
                    System.out.println("删除:" + JSONUtil.toJsonStr(operatorResult.getResult().getResponseStatus()));
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }

    }


}

